package com.itheima.day20.demo07;

import java.io.*;
import java.util.ArrayList;

public class ObjectPutStreamDemo {
    public static final String SRC = "./homework/src/com/itheima/day20/demo06/1.txt";
    public static final String DEST = "./homework/src/com/itheima/day20/demo06/ObjectPutStreamDemoDEST.txt";

    public static void main(String[] args) throws Exception {
        ArrayList<Person> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Person("jack", i));
        }

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(DEST)));
        oos.writeObject(list);

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(DEST)));
        Object o = ois.readObject();
        ArrayList<Person> list1 = (ArrayList<Person>) o;
        for (Person person : list1) {
            System.out.println(person);
        }
        ois.close();
        oos.close();
    }
}

class Person implements Serializable {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }
}